// import Toast from '../miniprogram_npm/@vant/weapp/toast/toast';
function request(url, data, method) {
  return new Promise(function (resolve, reject) {
    if(!data.user_id){
      data.user_id = wx.getStorageSync('user_id')
    }
    wx.request({
      url: url,
      data: data,
      method: method,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        // 'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.errcode == 105) { 
          Toast('我是提示文案，建议不超过十五字~');
           return
        }
        // if (res.data.code == 412) {
        //   wx.showToast({
        //     title: JSON.stringify(res.data.errmsg),
        //   })
        //   wx.navigateTo({
        //     url: '/pages/shouquan/shouquan',
        //   })
        //   return
        // }
        resolve(res.data);
        
      },
      fail: function (res) {
        reject(res);
        console.log("微信网络请求异常");
      }
    })
  }).catch((e) => {});
}

function jumpUrlForToken(Url) {
  console.log('token')
  wx.getStorage({
    key: 'token',
    success: function (res) {
      console.log(res.data)
      if (res.data == '') {
        wx.navigateTo({
          url: '/pages/shouquan/shouquan?backForTokenUrl=' + Url,
        })
      } else {
        wx.navigateTo({
          url: Url,
        })
      }


    },
    fail(err) {
      console.log(err)
      wx.navigateTo({
        url: '/pages/shouquan/shouquan?backForTokenUrl=' + Url,
      })
    }
  })
}

function showSuccessToast(msg) {
  wx.showToast({
    title: msg,
    icon: 'none',
    duration: 1500
  });
}

function showErrorToast(msg) {
  wx.showToast({
    title: msg,
    icon: 'none',
    duration: 1500
  });
}

// 判断是否是手机号码
function isMobile(str) {
  if (str.length != 0) {
    var reg = /^1[0-9][0-9]\d{8}$/;
    if ((reg.test(str))) {
      return true;
    } else {
      return false;
    }
  } else {
    return false;
  }
}

module.exports = {
  request, //封装的微信request
  showSuccessToast, //成功消息提示
  showErrorToast, //失败消息提示
  isMobile,
  jumpUrlForToken,
}